﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using RiskMan.DataClass.Models.Plaza_II;

namespace RiskMan.DataClass.Models.Indikators
{
    class IndikatorMacd
    {

        #region singleton
        protected static Object _lock = new Object();
        protected static IndikatorMacd _instance;

        public static IndikatorMacd Instance
        {
            get
            {
                lock (_lock)
                {
                    if (null == _instance)
                        _instance = new IndikatorMacd();
                    return _instance;
                }
            }
        }
        #endregion

        /// <summary>
        /// Расчет сигнальной линии
        /// </summary>
        /// <param name="price"></param>
        /// <param name="macd"></param>
        /// <param name="indexCol"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public double Average(double price, double macd, int indexCol, int period)
        {

            if (macd == 0)
                macd = price;
            else
            {
                macd = macd + (price - macd) * 2 / (period + 1);
            }

            return Math.Round(macd, 6);
        }
        /// <summary>
        /// Расчет макд
        /// </summary>
        /// <param name="price"></param>
        /// <param name="macd"></param>
        /// <param name="indexCol"></param>
        /// <param name="period"></param>
        /// <returns></returns>
        public double AverageMacd(double price, double macd, int indexCol, int period)
        {

            macd = macd + (price - macd) * 2 / (period + 1);

            return Math.Round(macd, 6);
        }

        /// <summary>
        /// расчет разницы между сигнальной линией и макд
        /// </summary>
        /// <param name="signal"></param>
        /// <param name="macd"></param>
        /// <returns></returns>
        public double MacdDiff(double signal, double macd)
        {
            return Math.Round(signal - macd, 6);
        }

        /// <summary>
        /// Счетчик
        /// </summary>
        /// <param name="macdDiffprev"></param>
        /// <param name="macdDiff"></param>
        /// <param name="macdCountprev"></param>
        /// <returns></returns>
        public int Count(double macdDiffprev, double macdDiff, int macdCountprev)
        {
            int macdCount=0;
            if (macdDiff > 0)
            {
                if (macdDiffprev < 0)
                    macdCount = 1;
                else
                    macdCount = macdCountprev + 1;
            }
            if (macdDiff < 0)
            {
                if (macdDiffprev > 0)
                    macdCount = -1;
                else
                    macdCount = macdCountprev - 1;
            }

            return macdCount;
        }
    }
}
